package com.dinian.zeus.controller;

import com.dinian.zeus.domain.Newspaper;
import com.dinian.zeus.service.NewspaperService;
import com.dinian.zeus.util.Result;
import com.dinian.zeus.util.commonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: dinian
 * @Date: 2018/11/18
 */
@Slf4j
@RestController
public class NewspaperController {

    @Autowired
    private NewspaperService newspaperService;

    /**
     * 初始化所有报刊信息
     * @return
     */
    @RequestMapping("/find/allNewspaper")
    public List<Newspaper> initNewspaper(){
        log.info("init Newspaper is start...");
        List<Newspaper> initNewspaper = newspaperService.findAllNewspaper();
        return initNewspaper;
    }

    /**
     * 添加
     * @param newspaper
     * @return
     */
    @RequestMapping("/addNewspaper")
    public Result addNewspaper(Newspaper newspaper){
        int i = newspaperService.addNewspaper(newspaper);
        if (i != 0){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 查询 byID
     * @param id
     * @return
     */
    @RequestMapping("/queryNewspaperById")
    public Result queryNewspaper(String id){
        Newspaper newspaper = newspaperService.queryNewspaperById(id);
        return Result.ok(newspaper);
    }

    /**
     * 修改
     * @param newspaper
     * @return
     */
    @RequestMapping("/modifyNewspaper")
    public Result modifyNewspaper(Newspaper newspaper){
        log.info(newspaper.toString());
        newspaperService.modifyNewspaper(newspaper);
        return Result.ok("修改成功");
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequestMapping("/deleteNewspaper")
    public Result removeNewspaper(String ids){
        log.info("delete id: {}",ids);
        String[] strings = ids.split(",");
        newspaperService.removeNewspapers(commonUtil.arrString2Int(strings));
        return Result.ok("删除成功");
    }
}
